package com.pxmeta.dynamic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxmeta.common.exception.ServiceException;
import com.pxmeta.dynamic.entity.Dynamic;
import com.pxmeta.dynamic.entity.CommunityDynamicUserLike;
import com.pxmeta.dynamic.entity.dto.DynamicLikeDTO;
import com.pxmeta.dynamic.entity.enums.MailInteractiveTypeEnum;
import com.pxmeta.dynamic.mapper.DynamicMapper;
import com.pxmeta.dynamic.mapper.CommunityDynamicUserLikeMapper;
import com.pxmeta.dynamic.service.ICommunityDynamicUserLikeService;
import com.pxmeta.dynamic.service.IMailService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 动态用户点赞表 服务实现类
 * </p>
 *
 * @author wcl
 * @since 2022-06-26
 */
@Service
@RequiredArgsConstructor
public class CommunityDynamicUserLikeServiceImpl extends ServiceImpl<CommunityDynamicUserLikeMapper, CommunityDynamicUserLike> implements ICommunityDynamicUserLikeService {

    private final IMailService mailService;

    private final DynamicMapper dynamicMapper;

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void like(DynamicLikeDTO dto, Long userId) {

        Long dynamicId = dto.getDynamicId();
        Dynamic dynamic = dynamicMapper.selectById(dynamicId);
        if (Objects.isNull(dynamic)) {
            throw new ServiceException("动态不存在");
        }
        Long likeCount = dynamic.getLikeCount();
        //如果是喜欢则删除喜欢记录
        CommunityDynamicUserLike userLike = findUserLike(userId, dynamicId);
        if (ObjectUtils.isNotEmpty(userLike)){
            baseMapper.deleteById(userLike.getId());
            likeCount--;
            dynamic.setLikeCount(likeCount);
            dynamicMapper.updateById(dynamic);
            return;
        }
        likeCount++;
        dynamic.setLikeCount(likeCount);
        dynamicMapper.updateById(dynamic);

        CommunityDynamicUserLike dynamicUserLike = new CommunityDynamicUserLike();
        dynamicUserLike.setDynamicId(dynamicId);
        dynamicUserLike.setUserId(userId);
        dynamicUserLike.setIsDel(0);
        dynamicUserLike.setCircleId(dynamic.getCircleId());
        baseMapper.insert(dynamicUserLike);

        //发送通知
        mailService.sendInteractMessage(MailInteractiveTypeEnum.LIKE.getType(), userId, dynamic.getUserId(), "",dynamic);
    }

    /**
     * 查询用户是否喜欢该动态
     * @param userId
     * @param dynamicId
     * @return
     */
    @Override
    public CommunityDynamicUserLike findUserLike(Long userId, Long dynamicId) {
        LambdaQueryWrapper<CommunityDynamicUserLike> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CommunityDynamicUserLike::getUserId, userId)
                .eq(CommunityDynamicUserLike::getDynamicId, dynamicId);
        return this.getOne(wrapper,false);
    }

    @Override
    public List<Long> getUserLikeIds(Long userId) {
        LambdaQueryWrapper<CommunityDynamicUserLike> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CommunityDynamicUserLike::getUserId,userId);

        return this.list(wrapper).stream().map(CommunityDynamicUserLike::getDynamicId).collect(Collectors.toList());
    }
}
